package com.zby.demo.controller.back;

import com.zby.demo.dao.ClassDao;
import com.zby.demo.dao.CourseDao;
import com.zby.demo.dao.StudentDao;
import com.zby.demo.entity.Class;
import com.zby.demo.entity.Student;
import com.zby.demo.entity.StudentSelectCourses;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @Author ChrisPhantom
 * @CreateDate 2020/12/18 18:08
 */

@Controller
public class StudentController {
    @Autowired
    StudentDao studentDao;
    @Autowired
    ClassDao classDao;
    @Autowired
    CourseDao courseDao;

    /*
     * @Param: [model]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/18 18:11
     * @Description: 请求转发学生列表页面（所查学生顺序为按学号降序）
     */
    @RequestMapping("/back/student/list")
    public String toStudentList(Model model) {
        List<Student> students = studentDao.queryAllStudent();
        model.addAttribute("students", students);
        List<Class> classes = classDao.queryAllClass();
        model.addAttribute("classes", classes);
        return "back/student/list";
    }

    /*
     * @Param: [student]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/18 18:13
     * @Description: 增加学生，并重定向到学生列表页面处理（默认密码为zut+账号后6位）
     */
    @PostMapping("/back/student/add")
    public String addStudent(Student student) {
        student.setPassword("zut" + student.getStudentId().substring(student.getStudentId().length() - 6));
        studentDao.addStudent(student);
        classDao.addStudentToClass(student.getStudentClassId());
        return "redirect:/back/student/list";
    }

    /*
     * @Param: [studentId]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/18 18:13
     * @Description: 按学生ID删除用户及其选课记录并重定向到学生列表页面处理。注意点：删除学生之前需要删除其选课信息。
     */
    @GetMapping("/back/student/delete/{studentId}")
    public String deleteStudent(@PathVariable("studentId") String studentId) {
        String studentClassId = studentDao.queryStudentByUsername(studentId).getStudentClassId();
        courseDao.deleteSelectCourseById(studentId);
        studentDao.deleteStudent(studentId);
        classDao.deleteStudentFromClass(studentClassId);
        return "redirect:/back/student/list";
    }

    /*
     * @Param: [deleteall]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/23 21:31
     * @Description: 批量删除学生。注意点同上
     */
    @PostMapping("/back/student/delete")
    public String deleteStudents(@Param("deleteall") String deleteall) {
        String[] deleteStudents = deleteall.split(",");
        for (String studentId : deleteStudents) {
            String studentClassId = studentDao.queryStudentByUsername(studentId).getStudentClassId();
            courseDao.deleteSelectCourseById(studentId);
            studentDao.deleteStudent(studentId);
            classDao.deleteStudentFromClass(studentClassId);
        }
        return "redirect:/back/student/list";
    }

    /*
     * @Param: [student]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/18 18:13
     * @Description: 更新学生信息（不包括密码），并重定向到学生列表页面处理
     */
    @PostMapping("/back/student/info")
    public String updateStudent(Student student) {
        studentDao.updateStudentInfo(student);
        return "redirect:/back/student/list";
    }

    /*
     * @Param: [studentId]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/18 18:13
     * @Description: 按学生ID重置学生密码，默认密码为:zut+账号后6位，后重定向到学生列表页面处理
     */
    @GetMapping("/back/student/{studentId}")
    public String resetStudentPassword(@PathVariable("studentId") String studentId) {
        String password = "zut" + studentId.substring(studentId.length() - 6);
        studentDao.updateStudentPassword(studentId, password);
        return "redirect:/back/student/list";
    }

    /*
     * @Param: [model]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/19 23:05
     * @Description: 请求转发到学生选课详情页面
     */
    @GetMapping("/back/student/selectcourse")
    public String toStudentSelectCourse(Model model) {
        List<StudentSelectCourses> selectcourses = studentDao.selectcourses();
        model.addAttribute("selectcourses", selectcourses);
        List<Class> classes = classDao.queryAllClass();
        model.addAttribute("classes", classes);
        return "back/student/selectcourse";
    }
}
